package com.ruoyi.shop.controller;

import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.shop.domain.ShopRestaurant;
import com.ruoyi.shop.service.IShopRestaurantService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 美食店铺Controller
 *
 * @author yangye
 * @date 2025-07-25
 */
@Api("美食店铺管理")
@RestController
@RequestMapping("/shop/restaurant")
public class ShopRestaurantController extends BaseController {
    @Autowired
    private IShopRestaurantService shopRestaurantService;

    /**
     * 抽取店铺
     */
    @ApiOperation("随机抽取店铺")
    @PreAuthorize("@ss.hasPermi('shop:restaurant:drawprize')")
    @GetMapping("/drawprize")
    public AjaxResult drawPrize(ShopRestaurant shopRestaurant) {
        List<ShopRestaurant> list = shopRestaurantService.selectShopRestaurantList(shopRestaurant);
        // 检查列表是否为空
        if (list == null || list.isEmpty()) {
            return error("没有可抽取的数据");
        }
        // 生成随机索引
        Collections.shuffle(list);
        return success(list.get(0));
    }

    /**
     * 查询美食店铺列表
     */
    @ApiOperation("查询店铺")
    @PreAuthorize("@ss.hasPermi('shop:restaurant:list')")
    @GetMapping("/list")
    public TableDataInfo list(ShopRestaurant shopRestaurant) {
        startPage();
        List<ShopRestaurant> list = shopRestaurantService.selectShopRestaurantList(shopRestaurant);
        return getDataTable(list);
    }

    /**
     * 导出美食店铺列表
     */
    @ApiOperation("导出店铺列表")
    @PreAuthorize("@ss.hasPermi('shop:restaurant:export')")
    @Log(title = "美食店铺", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShopRestaurant shopRestaurant) {
        List<ShopRestaurant> list = shopRestaurantService.selectShopRestaurantList(shopRestaurant);
        ExcelUtil<ShopRestaurant> util = new ExcelUtil<ShopRestaurant>(ShopRestaurant.class);
        util.exportExcel(response, list, "美食店铺数据");
    }

    /**
     * 获取美食店铺详细信息
     */
    @ApiOperation("获取店铺详情")
    @PreAuthorize("@ss.hasPermi('shop:restaurant:query')")
    @GetMapping(value = "/{restaurantId}")
    public AjaxResult getInfo(@PathVariable("restaurantId") Long restaurantId) {
        return success(shopRestaurantService.selectShopRestaurantByRestaurantId(restaurantId));
    }
    /**
     * 新增美食店铺
     */
    @ApiOperation("新增店铺")
    @PreAuthorize("@ss.hasPermi('shop:restaurant:add')")
    @Log(title = "美食店铺", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShopRestaurant shopRestaurant) {
        return toAjax(shopRestaurantService.insertShopRestaurant(shopRestaurant));
    }

    /**
     * 修改美食店铺
     */
    @ApiOperation("修改店铺")
    @PreAuthorize("@ss.hasPermi('shop:restaurant:edit')")
    @Log(title = "美食店铺", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShopRestaurant shopRestaurant) {
        return toAjax(shopRestaurantService.updateShopRestaurant(shopRestaurant));
    }
    /**
     * 删除美食店铺
     */
    @ApiOperation("删除店铺")
    @PreAuthorize("@ss.hasPermi('shop:restaurant:remove')")
    @Log(title = "美食店铺", businessType = BusinessType.DELETE)
    @DeleteMapping("/{restaurantIds}")
    public AjaxResult remove(@PathVariable Long[] restaurantIds) {
        return toAjax(shopRestaurantService.deleteShopRestaurantByRestaurantIds(restaurantIds));
    }
}
